{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "### 2.3.1. 标量"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "a732aa4d0c83e115"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(tensor(5.), tensor(6.), tensor(1.5000), tensor(9.))"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "\n",
    "x = torch.tensor(3.0)\n",
    "y = torch.tensor(2.0)\n",
    "\n",
    "x + y, x * y, x / y, x ** y"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T02:54:32.467407Z",
     "start_time": "2024-03-28T02:54:29.980547Z"
    }
   },
   "id": "980f5e4d4f1aacaf",
   "execution_count": 1
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "torch.Size([])"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T02:54:42.788520Z",
     "start_time": "2024-03-28T02:54:42.770463Z"
    }
   },
   "id": "43513dd40877e46e",
   "execution_count": 2
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.3.2. 向量"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f3e4c72ab79ff745"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([0, 1, 2, 3])"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.arange(4)\n",
    "x"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T02:56:14.782578Z",
     "start_time": "2024-03-28T02:56:14.770773Z"
    }
   },
   "id": "10be1574bf51b78c",
   "execution_count": 3
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor(3)"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[3]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T02:56:38.333025Z",
     "start_time": "2024-03-28T02:56:38.322008Z"
    }
   },
   "id": "f5adaebe443e1d4d",
   "execution_count": 4
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### 2.3.2.1. 长度、维度和形状"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "dfd66b2c858d2559"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "4"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(x)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T02:57:20.014610Z",
     "start_time": "2024-03-28T02:57:20.007044Z"
    }
   },
   "id": "5a3353cf11732e68",
   "execution_count": 5
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "torch.Size([4])"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T02:57:38.591018Z",
     "start_time": "2024-03-28T02:57:38.572191Z"
    }
   },
   "id": "44ea0c818a681602",
   "execution_count": 6
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.3.3. 矩阵"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4f80583150b12795"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[ 0,  1,  2,  3],\n        [ 4,  5,  6,  7],\n        [ 8,  9, 10, 11],\n        [12, 13, 14, 15],\n        [16, 17, 18, 19]])"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = torch.arange(20).reshape(5, 4)\n",
    "A"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:00:02.374115Z",
     "start_time": "2024-03-28T03:00:02.359125Z"
    }
   },
   "id": "a7bc328fe1b54405",
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[ 0,  4,  8, 12, 16],\n        [ 1,  5,  9, 13, 17],\n        [ 2,  6, 10, 14, 18],\n        [ 3,  7, 11, 15, 19]])"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.T"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:01:12.770064Z",
     "start_time": "2024-03-28T03:01:12.761070Z"
    }
   },
   "id": "570999257fcf4c87",
   "execution_count": 8
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[1, 2, 3],\n        [2, 0, 4],\n        [3, 4, 5]])"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B = torch.tensor([[1, 2, 3], [2, 0, 4], [3, 4, 5]])\n",
    "B"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:01:27.229938Z",
     "start_time": "2024-03-28T03:01:27.205318Z"
    }
   },
   "id": "c9d00389f50867db",
   "execution_count": 9
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[True, True, True],\n        [True, True, True],\n        [True, True, True]])"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B == B.T"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:01:33.892229Z",
     "start_time": "2024-03-28T03:01:33.870590Z"
    }
   },
   "id": "949491595d0b8cb5",
   "execution_count": 10
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.3.4. 张量"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "54689a56c9fe90b4"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[[ 0,  1,  2,  3],\n         [ 4,  5,  6,  7],\n         [ 8,  9, 10, 11]],\n\n        [[12, 13, 14, 15],\n         [16, 17, 18, 19],\n         [20, 21, 22, 23]]])"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = torch.arange(24).reshape(2, 3, 4)\n",
    "X"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:03:01.905680Z",
     "start_time": "2024-03-28T03:03:01.895701Z"
    }
   },
   "id": "f9134f8be6bf111b",
   "execution_count": 11
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.3.5. 张量算法的基本性质"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "62b7e7e78c19688e"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(tensor([[ 0.,  1.,  2.,  3.],\n         [ 4.,  5.,  6.,  7.],\n         [ 8.,  9., 10., 11.],\n         [12., 13., 14., 15.],\n         [16., 17., 18., 19.]]),\n tensor([[ 0.,  2.,  4.,  6.],\n         [ 8., 10., 12., 14.],\n         [16., 18., 20., 22.],\n         [24., 26., 28., 30.],\n         [32., 34., 36., 38.]]))"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = torch.arange(20, dtype=torch.float32).reshape(5, 4)\n",
    "B = A.clone()  # 通过分配新内存，将A的一个副本分配给B\n",
    "A, A + B"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:04:08.740474Z",
     "start_time": "2024-03-28T03:04:08.718739Z"
    }
   },
   "id": "64c17e0bb1397a76",
   "execution_count": 13
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[ 0.,  1.,  2.,  3.],\n        [ 4.,  5.,  6.,  7.],\n        [ 8.,  9., 10., 11.],\n        [12., 13., 14., 15.],\n        [16., 17., 18., 19.]])"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C = A\n",
    "C"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:04:32.917277Z",
     "start_time": "2024-03-28T03:04:32.895727Z"
    }
   },
   "id": "ca4eb2109e61042e",
   "execution_count": 14
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[ 0.,  2.,  4.,  6.],\n        [ 8., 10., 12., 14.],\n        [16., 18., 20., 22.],\n        [24., 26., 28., 30.],\n        [32., 34., 36., 38.]])"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A + C"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:04:38.131164Z",
     "start_time": "2024-03-28T03:04:38.124659Z"
    }
   },
   "id": "6c3f2d8a5fae1422",
   "execution_count": 15
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(2740596063984, 2740596063344, 2740596063984)"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id(A), id(B), id(C)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:04:54.752096Z",
     "start_time": "2024-03-28T03:04:54.729428Z"
    }
   },
   "id": "bb880d2d28012785",
   "execution_count": 16
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[  0.,   1.,   4.,   9.],\n        [ 16.,  25.,  36.,  49.],\n        [ 64.,  81., 100., 121.],\n        [144., 169., 196., 225.],\n        [256., 289., 324., 361.]])"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A * B"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:05:28.974680Z",
     "start_time": "2024-03-28T03:05:28.964129Z"
    }
   },
   "id": "fcc793e4528a5168",
   "execution_count": 17
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(tensor([[[ 2,  3,  4,  5],\n          [ 6,  7,  8,  9],\n          [10, 11, 12, 13]],\n \n         [[14, 15, 16, 17],\n          [18, 19, 20, 21],\n          [22, 23, 24, 25]]]),\n torch.Size([2, 3, 4]))"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 2\n",
    "X = torch.arange(24).reshape(2, 3, 4)\n",
    "a + X, (a * X).shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:05:41.669019Z",
     "start_time": "2024-03-28T03:05:41.652463Z"
    }
   },
   "id": "4c9d497fe6a04ddc",
   "execution_count": 18
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.3.6. 降维"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "182d881814b6079f"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(tensor([0., 1., 2., 3.]), tensor(6.))"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.arange(4, dtype=torch.float32)\n",
    "x, x.sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:06:21.699951Z",
     "start_time": "2024-03-28T03:06:21.680909Z"
    }
   },
   "id": "ca5b9c229e7e35d5",
   "execution_count": 19
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(torch.Size([5, 4]), tensor(190.))"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.shape, A.sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:06:52.122911Z",
     "start_time": "2024-03-28T03:06:52.104048Z"
    }
   },
   "id": "3f60591a237391e7",
   "execution_count": 20
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(tensor([40., 45., 50., 55.]), torch.Size([4]))"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_sum_axis0 = A.sum(axis=0)\n",
    "A_sum_axis0, A_sum_axis0.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:07:25.893745Z",
     "start_time": "2024-03-28T03:07:25.880697Z"
    }
   },
   "id": "9dbe06305bc748b5",
   "execution_count": 21
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(tensor([ 6., 22., 38., 54., 70.]), torch.Size([5]))"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_sum_axis1 = A.sum(axis=1)\n",
    "A_sum_axis1, A_sum_axis1.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:07:34.281856Z",
     "start_time": "2024-03-28T03:07:34.262562Z"
    }
   },
   "id": "8c5b9606d5d0bf5e",
   "execution_count": 22
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor(190.)"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.sum(axis=[0, 1])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:07:45.862483Z",
     "start_time": "2024-03-28T03:07:45.842618Z"
    }
   },
   "id": "8f18a518f55bf352",
   "execution_count": 23
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(tensor(9.5000), tensor(9.5000))"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.mean(), A.sum() / A.numel()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:07:59.215434Z",
     "start_time": "2024-03-28T03:07:59.200167Z"
    }
   },
   "id": "fcd0a95305861fb7",
   "execution_count": 24
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(tensor([ 8.,  9., 10., 11.]), tensor([ 8.,  9., 10., 11.]))"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.mean(axis=0), A.sum(axis=0) / A.shape[0]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:08:15.428714Z",
     "start_time": "2024-03-28T03:08:15.410421Z"
    }
   },
   "id": "bedb90dbd3416b15",
   "execution_count": 25
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### 2.3.6.1. 非降维求和"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "61dbd09a792636c5"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[ 0.,  1.,  2.,  3.],\n        [ 4.,  5.,  6.,  7.],\n        [ 8.,  9., 10., 11.],\n        [12., 13., 14., 15.],\n        [16., 17., 18., 19.]])"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:17:30.835361Z",
     "start_time": "2024-03-28T03:17:30.827824Z"
    }
   },
   "id": "41545fadcbc5219e",
   "execution_count": 27
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[ 6.],\n        [22.],\n        [38.],\n        [54.],\n        [70.]])"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum_A = A.sum(axis=1, keepdims=True)\n",
    "sum_A"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:08:39.204413Z",
     "start_time": "2024-03-28T03:08:39.188066Z"
    }
   },
   "id": "4d8138bbdfda58f8",
   "execution_count": 26
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[0.0000, 0.1667, 0.3333, 0.5000],\n        [0.1818, 0.2273, 0.2727, 0.3182],\n        [0.2105, 0.2368, 0.2632, 0.2895],\n        [0.2222, 0.2407, 0.2593, 0.2778],\n        [0.2286, 0.2429, 0.2571, 0.2714]])"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A / sum_A"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:17:56.450481Z",
     "start_time": "2024-03-28T03:17:56.434686Z"
    }
   },
   "id": "7b9f96a7be9eab1c",
   "execution_count": 28
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[ 0.,  1.,  2.,  3.],\n        [ 4.,  6.,  8., 10.],\n        [12., 15., 18., 21.],\n        [24., 28., 32., 36.],\n        [40., 45., 50., 55.]])"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.cumsum(axis=0)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:18:52.669017Z",
     "start_time": "2024-03-28T03:18:52.660276Z"
    }
   },
   "id": "28d352ac8302954a",
   "execution_count": 29
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.3.7. 点积（Dot Product）"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "d6aea38b10310e23"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(tensor([0., 1., 2., 3.]), tensor([1., 1., 1., 1.]), tensor(6.))"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = torch.ones(4, dtype = torch.float32)\n",
    "x, y, torch.dot(x, y)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:19:55.458261Z",
     "start_time": "2024-03-28T03:19:55.438777Z"
    }
   },
   "id": "82c8b0b8aaf764af",
   "execution_count": 30
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor(6.)"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.sum(x * y)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:20:11.563424Z",
     "start_time": "2024-03-28T03:20:11.548299Z"
    }
   },
   "id": "60b2bc8c3778b4cb",
   "execution_count": 31
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.3.8. 矩阵-向量积"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "6aaf457a62d94888"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(torch.Size([5, 4]), torch.Size([4]), tensor([ 14.,  38.,  62.,  86., 110.]))"
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.shape, x.shape, torch.mv(A, x)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:23:24.976137Z",
     "start_time": "2024-03-28T03:23:24.954498Z"
    }
   },
   "id": "af518c98f749f97d",
   "execution_count": 32
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.3.9. 矩阵-矩阵乘法"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "6c38ae943ab94423"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(tensor([[ 6.,  6.,  6.],\n         [22., 22., 22.],\n         [38., 38., 38.],\n         [54., 54., 54.],\n         [70., 70., 70.]]),\n tensor([[ 6.,  6.,  6.],\n         [22., 22., 22.],\n         [38., 38., 38.],\n         [54., 54., 54.],\n         [70., 70., 70.]]))"
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B = torch.ones(4, 3)\n",
    "torch.mm(A, B), torch.matmul(A, B)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:32:39.382600Z",
     "start_time": "2024-03-28T03:32:39.355637Z"
    }
   },
   "id": "db9e411e42590bbe",
   "execution_count": 37
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.3.10. 范数"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "5341b24200e05902"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor(5.)"
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u = torch.tensor([3.0, -4.0])\n",
    "torch.norm(u)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:28:56.401574Z",
     "start_time": "2024-03-28T03:28:56.384989Z"
    }
   },
   "id": "500af87afdf89601",
   "execution_count": 34
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor(7.)"
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.abs(u).sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:29:27.658564Z",
     "start_time": "2024-03-28T03:29:27.645974Z"
    }
   },
   "id": "db6908ea08590d1c",
   "execution_count": 35
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor(6.)"
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.norm(torch.ones((4, 9)))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-28T03:30:24.709857Z",
     "start_time": "2024-03-28T03:30:24.696014Z"
    }
   },
   "id": "7bbf2fd8155ed47",
   "execution_count": 36
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### 2.3.10.1. 范数和目标\n",
    "在深度学习中，我们经常试图解决优化问题： 最大化分配给观测数据的概率; 最小化预测和真实观测之间的距离。 用向量表示物品（如单词、产品或新闻文章），以便最小化相似项目之间的距离，最大化不同项目之间的距离。 目标，或许是深度学习算法最重要的组成部分（除了数据），通常被表达为范数。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "353bf14fd738be4c"
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### 2.3.11. 关于线性代数的更多信息\n",
    "仅用一节，我们就教会了阅读本书所需的、用以理解现代深度学习的线性代数。 线性代数还有很多，其中很多数学对于机器学习非常有用。 例如，矩阵可以分解为因子，这些分解可以显示真实世界数据集中的低维结构。 机器学习的整个子领域都侧重于使用矩阵分解及其向高阶张量的泛化，来发现数据集中的结构并解决预测问题。 当开始动手尝试并在真实数据集上应用了有效的机器学习模型，你会更倾向于学习更多数学。 因此，这一节到此结束，本书将在后面介绍更多数学知识。\n",
    "\n",
    "如果渴望了解有关线性代数的更多信息，可以参考线性代数运算的在线附录或其他优秀资源 (Kolter, 2008, Petersen et al., 2008, Strang, 1993)。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "72fe8af4d6d8691d"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "d5826f874ac26d22"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
